import java.util.ArrayList;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.io.*;
import java.text.DecimalFormat;


public class Item implements Entity, Serializable {

	protected String DisplayName;
	protected String Name;
	protected String Description;
	protected String Orientation;
	protected ArrayList<Entity> Inventory = new ArrayList<Entity>();
	protected boolean CanBePickedUp = true;
	protected String Why;
	protected boolean isVisible;
	protected int value;
	protected double Weight;
	protected Entity key;
	protected boolean isBed = false;
	protected int Gold;
	protected boolean forSale;
	protected DecimalFormat df = new DecimalFormat("#.#");
	
	public Item(String a, String b, String c, String d, double e, int ... values) {
		Orientation = a;
		DisplayName = b;
		Name = c;
		Description = d;
		CanBePickedUp = true;
		isVisible = true;
		if (values.length == 0) {
			value = 0;
		} else {
			value = values[0];
		}
		Weight = e;
		Gold = 0;
		forSale = false;
	}
	
	public void modifyGold(int a) {
		Gold = Gold + a;
	}
	
	public void forSale(boolean a) {
		forSale = a;
	}
	
	public boolean isForSale() {
		return forSale;
	}
	
	public int getGold() {
		return Gold;
	}
	
	public void setGold(int a) {
		Gold = 0;
	}
	
	public double getWeight() {
		return Weight;
	}
	
	public int getValue() {
		return value;
	}
	
	public void setValue(int a) {
		value = a;
	}
	
	public void addEntity(Entity a) {
		Inventory.add(a);
	}
	
	public ArrayList<Entity> getInventory() {
		return Inventory;
	}
	
	public boolean isLocked() {
		return false;
	}
	
	public void removeObject(Entity a) {
		Inventory.remove(a);
	}
	
	public void Describe(JTextArea y) {
		y.append(Description + "." + "\n\n");
		y.append("Weight: " + df.format(Weight) + "\n");
		if (forSale == false) {
			y.append("Value: " + value);
		} else {
			y.append("Price: " + value);
		}
	}
	
	
	public void Display(JTextArea y) {
		y.append(Orientation + " is " + DisplayName);
	}
	
	public String getOrientation() {
		return Orientation;
	}
	
	public void setOrientation(String a) {
		Orientation = a;
	}
	
	public String getDisplayName(){
		return DisplayName;
	}
	
	public String getName() {
		return Name;
	}
	
	public String getDescription() {
		return Description;
	}
	
	public void Use(Player a, String b, JTextArea y, JTextField x) {	
		
		Entity target = a.getCurrentLocation().findObject(b);
		
		if (target == null) {
			y.append("You can't use the " + Name + " on that!\n");
		} else {
		
		if (target.getKey() == this) {
		
			if (target.isLocked() == true) {
				target.unlock();
				y.append("You unlock the " + target.getName() + " with the " + Name + ".\n\n");
			} else if (target.isLocked() == false) {
				target.lock();
				y.append("You lock the " + target.getName() + " with the " + Name + ".\n\n");
			}
		} else {
			y.append("You rattle the " + Name + " around in the lock but it doesn't fit.\n\n");
		}
	}
		
	}
	
	public boolean TestCanBePickedUp() {
		return CanBePickedUp;
	}
	
	public void CantBePickedUp(String b) {
		CanBePickedUp = false;
		Why = b;		
	}
	
	public String CanBePickedUp_Why() {
		return Why;
	}
	
	public void printInventory(JTextArea y) {
		y.append("Searching the " + DisplayName + " you find the following items:\n\n");
		for (Entity a : Inventory) {
			y.append(a.getName());
		}
	}
	
	public void setVisible(boolean a) {
		isVisible = a;
	}

	public boolean isVisible() {
		return isVisible;
	}
	
	public void setDisplayName(String a) {
		DisplayName = a;
	}
	
	public void setContentsVisible(boolean a) {
		for (Entity b : Inventory) {
			b.setVisible(true);
		}
	}
	
	public Entity getKey() {
		return key;
	}
	
	public void lock() {
		
	}
	
	public void unlock() {
		
	}

	public boolean isBed() {
		return isBed;
	}
	
	public void isBed(boolean a) {
		isBed = a;
	}
	
	
}
